<template>
	<view>
		<view class="padding">
			<view class="padding-sm bg-white radius-xl address-item margin-bottom" v-for="(item,index) in list" :key="index" :class="item.is_default === 1?'default-border':''">
				<view class="content" @click="toOrder(item)">
					<view class="username margin-bottom-sm">
						<text class="text-bold text-black padding-right">{{item.nickname}}</text>
						<text>{{item.phone}}</text>
					</view>
					<view class="detail">
            <div class="text-gray text-sm">{{item.address_name || ''}}</div>
            <div class="text-gray text-sm">{{item.address}}</div>
					</view>
				</view>
				<div style="display: flex;align-items: center">
          <view class="edit" @click.stop="toUrl('/pages/user/address/save?pageType=edit&addressId='+item.id,true)">
            <view class="cuIcon-edit"></view>
          </view>
          <view class="edit" style="margin-left: 10rpx" @click.stop="deleteAddress(item.id)">
            <view class="cuIcon-delete"></view>
          </view>
        </div>
				<view class="default">
					<view>默认</view>
				</view>
			</view>

			<view class="padding margin-top-lg">
				<view class="flex flex-direction">
					<button class="cu-btn china-btn-bg lg round cuIcon-add" @click="toUrl('/pages/user/address/save?pageType=add',true)">添加地址</button>
				</view>
			</view>
		</view>

		<Loading v-if="pageLoading"/>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				pageLoading:false,
				current_page:1,
				list: [],
				pageType:'list',
        userInfo: {}
			};
		},
		onLoad(options) {
			if(options.pageType){
				this.pageType = options.pageType;
			}


		},
    onShow() {
      this.userInfo = uni.getStorageSync('userInfo') || {
      }
      this.getList()
    },
		methods:{
      getList() {
        if (this.userInfo.id) {
          this.pageLoading = true
          this.$http.getAddressList({ member_id: this.userInfo.id }).then(res => {
            this.list = res
          }).finally(() => {
            this.pageLoading = false
          })
        }
      },
			toOrder(item){
				if(this.pageType == 'order'){
					uni.$emit('address', item);
					uni.navigateBack();
				}
			},
      deleteAddress(id) {
        uni.showModal({
          title: '提示',
          content: '确定删除该地址吗？',
          success: (res) => {
            if (res.confirm) {
              this.$http.deleteAddress(id).then(() => {
                uni.showToast({
                  title: '删除成功',
                  icon: 'none'
                })
                this.getList()
              })
            }
          }
        })
      }

		}
	}
</script>

<style lang="less" scoped>
.address-item{
	display: flex;
	align-items: center;
	justify-content: space-between;
	.default{
		display: none;
	}
}
.default-border{
	position: relative;
	border: 1upx solid #ff6600;
	.default{
		display: block;
		position: absolute;
		right: -2upx;
		background: #ff6600;
		color: white;
		top: -2upx;
		border-top-right-radius: 25upx;
		padding: 6upx 12upx;
		font-size: 24upx;
		border-bottom-left-radius: 10upx;
	}

}
</style>
